// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
export function TMap(key) {
  // return new Promise(function(resolve, reject) {
  //   // window.init = function() {
  //   //   resolve(window.qq); // 关键
  //   // };
  //   // var script = document.createElement("script");
  //   // script.type = "text/javascript";
  //   // script.src = `https://map.qq.com/api/js?v=2.exp&callback=init&key=${key}`;
  //   // script.onerror = reject;
  //   // document.head.appendChild(script);

  //   var script2 = document.createElement("script");
  //   script2.type = "text/javascript";
  //   script2.src = `https://map.qq.com/api/gljs?v=1.exp&key=${key}`;
  //   script2.onload = () => resolve(window.TMap); // 直接返回 TMap 对象
  //   script2.onerror = reject;
  //   document.head.appendChild(script2);
  // });
  return new Promise(function(resolve, reject) {
    // 加载 GL JS v1 并指定 service 库（地理编码需要）
    const script = document.createElement("script");
    script.type = "text/javascript";
    // 补充 libraries=service，否则无法使用 Geocoder
    script.src = `https://map.qq.com/api/gljs?v=1.exp&key=${key}&libraries=service`;
    // 脚本加载成功后，返回全局 TMap 对象（核心！）
    script.onload = () => resolve(window.TMap);
    // 加载失败时 reject
    script.onerror = () => reject(new Error("地图脚本加载失败"));
    document.head.appendChild(script);
  });
}
